<?php

namespace app\admin\controller\auth;

use app\admin\controller\Base;
use app\admin\model\Admin;
use app\common\library\Hash;
use think\Db;

/**
 * 菜单规则管理
 * Class RuleController
 * @package app\admin\controller\auth
 */
class Admins extends Base
{
    /**
     * 主页面
     * @return mixed
     */
    public function index()
    {
        return view();
    }

    /**
     * 列表
     * @return mixed
     */
    public function lists()
    {

        $map = array();
        $data = array();
        $post = $this->request->param();
        if (isset($post['keywork'])) {
            $map[] = ['name', 'like', '%' . $post['keywork'] . '%'];
        }
        //dump($map);
        $data['count'] = Db::view('admin', 'id,username,nickname,login_times,last_login_time,last_login_ip,status,create_time')
            ->view('auth_group_access', ['group_id' => 'acc_ggroup_id'], 'admin.id=auth_group_access.uid')
            ->view('auth_group', ['title' => 'group_name', 'id' => 'group_id'], 'auth_group_access.group_id=auth_group.id')
            ->where($map)->count();
        // echo Db::name('goods')->where($map)->fetchSql(true)->select();
        $data['data'] = Db::view('admin', 'id,username,nickname,password,login_times,last_login_time,last_login_ip,status,create_time,openid')
            ->view('auth_group_access', ['group_id' => 'acc_ggroup_id'], 'admin.id=auth_group_access.uid')
            ->view('auth_group', ['title' => 'group_name', 'id' => 'group_id'], 'auth_group_access.group_id=auth_group.id')
            ->where($map)
            ->order(['admin.status'=>'desc','id'=>'desc'])->page(input("page"), input('limit'))->select();
        foreach ($data['data'] as &$info){
            $info['openid']=$info['openid']?'是':'否';
        }
        if ($data['count'] > 0 && $data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);

    }

    /**
     * 添加页面和添加操作
     * @return mixed
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();
            if (Db::name('admin')->where('username', $post['username'])->find() !== null) {
                $this->error('登录名已经使用,请更换其他登录名');
            }
            if (!$post['password']) {
                $post['password'] = $post['username'];
            }

            $validate = new \app\admin\validate\Admin();
            if (!$validate->check($post)) {
                $this->error($validate->getError());
            }

            $post['password'] = Hash::hash($post['password']);
            $admin = new Admin();
            if ($admin->allowField(true)->save($post) !== false) {
                if (!empty($post['group'])) {
                    $groups = [];
                    foreach ($post['group'] as &$item) {
                        $data = [];
                        $data['uid'] = $admin->id;
                        $data['group_id'] = $item;
                        $groups[] = $data;
                    }
                    Db::name('auth_group_access')->insertAll($groups);
                }
                $this->success('添加成功');
            }
            $this->error('添加失败');
        } else {
            //获取用户组
            $group = Db::name('auth_group')->field('id,title,status')->where('id', 'neq', '1')->select();
            $this->assign('group', $group);
            return view('edit');
        }
    }

    /**
     * 修改页面和修改操作
     * @return mixed
     */
    public function edit()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();
            if ($post['id'] == 1) {
                $this->error('超级管理员无法修改，请在个人信息页面修改');
            }
            $info = Db::name('admin')->where('username', $post['username'])->find();
            if ($info !== false && $info['id'] != $post['id']) {
                $this->error('登录名已经使用,请更换其他登录名');
            }
            if (!empty($post['password']) && $post['password']) {
                $post['password'] = Hash::hash($post['password']);
            } else {
                unset($post['password']);
            }

            $validate = new \app\admin\validate\Admin();
            if (!$validate->check($post)) {
                $this->error($validate->getError());
            }

            if ((new Admin())->allowField(true)->isUpdate(true)->save($post) !== false) {
                if (!empty($post['group'])) {
                    Db::name('auth_group_access')->where(['uid' => $post['id']])->delete();
                    $groups = [];
                    foreach ($post['group'] as &$item) {
                        $data = [];
                        $data['uid'] = $post['id'];
                        $data['group_id'] = $item;
                        $groups[] = $data;
                    }
                    Db::name('auth_group_access')->insertAll($groups);
                }
                $this->success('修改成功');
            }
            $this->error('修改失败');
        } else {
            $id = $this->request->param('id', 0, 'intval');
            if (!$id) {
                $this->error('参数错误');
            }
            $info = Admin::get($id);
            //获取该用户的用户组
            $access_group = Db::name('auth_group_access')->where(['uid' => $id])->column('group_id');
            $group = Db::name('auth_group')->field('id,title,status')->where('id', 'neq', '1')->select();
            $this->assign('group', $group);
            $this->assign('info', $info);
            $this->assign('access_group', $access_group);
            return view();
        }
    }

    /**
     * 删除操作
     * @return json
     */
    public function delete()
    {
        $id = $this->request->param('id', 0, 'intval');
        if ($id <= 0) {
            $this->error('参数错误');
        }
        if (Admin::destroy($id) !== false) {
            Db::name('auth_group_access')->where('uid', $id)->delete();
            $this->success('删除成功');
        }

        $this->error('删除失败');
    }

}
